using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CustomControls
{
    public enum ColorSchema
    {
        Transparent,
        Gradient,
        Normal
    }

    public class CustomPanel : Panel
    {
        private ColorSchema m_colorSchema = ColorSchema.Normal;
        [DefaultValue("Normal"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
        public ColorSchema ColorSchema
        {
            get { return m_colorSchema; }
            set
            {
                m_colorSchema = value;
                this.Invalidate();
            }
        }

        private LinearGradientMode m_linearGradientMode = LinearGradientMode.ForwardDiagonal;
        [DefaultValue("ForwardDiagonal"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
        public LinearGradientMode LinearGradientMode
        {
            get { return m_linearGradientMode; }
            set
            {
                m_linearGradientMode = value;
                this.Invalidate();
            }
        }

        private bool m_colorOrder = true;
        [DefaultValue("true"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
        public bool ColorOrder
        {
            get { return m_colorOrder; }
            set
            {
                m_colorOrder = value;
                this.Invalidate();
            }
        }

        private Color m_secondColor = Color.White;
        [DefaultValue("true"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
        public Color SecondColor
        {
            get { return m_secondColor; }
            set
            {
                m_secondColor = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (m_colorSchema == ColorSchema.Normal) base.OnPaint(e);
            else if (m_colorSchema == ColorSchema.Transparent) return;
            else if(this.Width > 0 && this.Height > 0)
            {
                Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
                LinearGradientBrush brushBackground = null;
                if (m_colorOrder)
                    brushBackground = new LinearGradientBrush(rect, this.BackColor, m_secondColor, m_linearGradientMode);
                else
                    brushBackground = new LinearGradientBrush(rect, m_secondColor, this.BackColor, m_linearGradientMode);
                e.Graphics.FillRectangle(brushBackground, rect);
                brushBackground.Dispose();
                base.OnPaint(e);
            }
        }
    }
}